Skip to content

Set content type to application/json for flask.make_response with JSON data#54

Closed
pixeebot[bot] wants to merge 1 commit intodevelopfrom
pixeebot/drip-2024-12-21-pixee-python/flask-json-response-type
Closed

Set content type to application/json for flask.make_response with JSON data#54
pixeebot[bot] wants to merge 1 commit intodevelopfrom
pixeebot/drip-2024-12-21-pixee-python/flask-json-response-type

Conversation

@pixeebot
Copy link
Contributor

@pixeebot pixeebot bot commented Dec 21, 2024

The default mimetype for make_response in Flask is 'text/html'. This is true even when the response contains JSON data.
If the JSON contains (unsanitized) user-supplied input, a malicious user may supply HTML code which leaves the application vulnerable to cross-site scripting (XSS).
This fix explicitly sets the response type to application/json when the response body is JSON data to avoid this vulnerability. Our changes look something like this:

from flask import make_response, Flask
import json

app = Flask(__name__)

@app.route("/test")
def foo(request):
    json_response = json.dumps({ "user_input": request.GET.get("input") })
-   return make_response(json_response)
+   return make_response(json_response, {'Content-Type':'application/json'})
More reading

I have additional improvements ready for this repo! If you want to see them, leave the comment:

@pixeebot next

... and I will open a new PR right away!

🧚🤖 Powered by Pixeebot

Feedback | Community | Docs | Codemod ID: pixee:python/flask-json-response-type

@pixeebot
Copy link
Contributor Author

pixeebot bot commented Dec 29, 2024

I'm confident in this change, but I'm not a maintainer of this project. Do you see any reason not to merge it?

If this change was not helpful, or you have suggestions for improvements, please let me know!

@pixeebot
Copy link
Contributor Author

pixeebot bot commented Dec 30, 2024

Just a friendly ping to remind you about this change. If there are concerns about it, we'd love to hear about them!

@pixeebot
Copy link
Contributor Author

pixeebot bot commented Jan 5, 2025

This change may not be a priority right now, so I'll close it. If there was something I could have done better, please let me know!

You can also customize me to make sure I'm working with you in the way you want.

@pixeebot pixeebot bot closed this Jan 5, 2025
@udhayprakash udhayprakash deleted the pixeebot/drip-2024-12-21-pixee-python/flask-json-response-type branch March 15, 2025 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants